package com.lz.rabbitmq.topic;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConsumerTopic {

    /**
     * 注册交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange("lz_boot_topic_exchange").build();
    }

    /**
     * 注册队列
     */
    @Bean
    public Queue topicQueue1() {
        return QueueBuilder.durable("lz_boot_topic_queue1").build();
    }

    @Bean
    public Queue topicQueue2() {
        return QueueBuilder.durable("lz_boot_topic_queue2").build();
    }

    @Bean
    public Queue topicQueue3() {
        return QueueBuilder.durable("lz_boot_topic_queue3").build();
    }

    @Bean
    public Binding topicBind1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("lz.#");
    }

    @Bean
    public Binding topicBind2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lz.*");
    }

    @Bean
    public Binding topicBind3() {
        return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("#");
    }

    @RabbitListener(queues = "lz_boot_topic_queue1")
    public void topicProcess1(String msg) {
        System.out.println("lz_boot_topic_queue1 接收到消息：" + msg);
    }

    @RabbitListener(queues = "lz_boot_topic_queue2")
    public void topicProcess2(String msg) {
        System.out.println("lz_boot_topic_queue2 接收到消息：" + msg);
    }

    @RabbitListener(queues = "lz_boot_topic_queue3")
    public void topicProcess3(String msg) {
        System.out.println("lz_boot_topic_queue3 接收到消息：" + msg);
    }
}
